package com.wan.data.mysql.orm.convert.auto;

import com.wan.data.mysql.orm.convert.AutoConverter;
import com.wan.data.mysql.orm.meta.ColumnMeta;
import com.wan.data.mysql.util.DbColumnType;

import java.util.concurrent.atomic.AtomicInteger;

public class IntegerConverter implements AutoConverter {
    @Override
    public Class<?>[] convertType() {
        return new Class[]{int.class,Integer.class, AtomicInteger.class};
    }

    @Override
    public DbColumnType getDbType() {
        return DbColumnType.INT;
    }

    @Override
    public Object dbToEntity(ColumnMeta columnMeta, Object value) throws Exception {
        if(columnMeta.getFieldType() == AtomicInteger.class){
            return new AtomicInteger((Integer) value);
        }
        return value;
    }

    @Override
    public Object entityToDb(ColumnMeta columnMeta, Object value) throws Exception {
        if(value instanceof AtomicInteger){
            AtomicInteger atomicInteger = (AtomicInteger) value;
            return atomicInteger.get();
        }
        return value;
    }
}
